<?php
class MProduct extends Model {
            
    function __construct() {
        parent::__construct();
        $this->table = "cms_product";         
    }
    
    //产品列表
    public function selectProductList(){
        $sql_data = array('is_delete'=>'0');
        $result = $this->selectAll($this->table, $sql_data,'-1','','','','id ASC');
        return $result;
    }

    //添加产品
    public  function add($data){
        $result = $this->insertRow($this->table, $data);
        return $result;
    }
    
    //批量添加产品
    public function mutiReplace($data)
    {
        return $this->_db->insertMulti($this->table, $data, array(), 'replace');
    }
    
    //更新产品
    public function update($data,$where){
        $result = $this->updateRow($this->table, $data, $where);
        return$result;
    }
    
    //根据id更新产品
    public function updateById($product_id,$data){
        $where = " where id='".$product_id."'";
        $result = $this->update($data, $where);
        return $result;
    }
    
    //更新产品库存
    public function updateCountById($product_id,$count,$oper='-'){
        $data = array('count'=>val('count')-$count);
        $result = $this->updateById($product_id, $data);
        return $result;
    }
    
    //批量更新产品库存
    public function updateCounts($data,$symbol='-'){
        $keys = array_keys($data);
        if(strcmp($symbol, '=') == 0){
            $count = " count= ";
        }  else {
            $count = " count=count".$symbol;
        }
        $sql = " update $this->table set $count case id";
        foreach ($data as $key => $value) {
            $sql .= " when $key then $value ";
        }
        $sql .= " end";
        $sql .= " where id in (".implode(',', $keys).")";
        $result = $this->_db->exec($sql);
        return $result;
    }
    
    //批量更新产品
    public function updateProducts($data,$index,$condition){
        $result = $this->_db->updateMulti($this->table,$data,$index,$condition);
        return $result;
    }
}

